package net.xxcl.kygl.service;

import net.xxcl.kygl.mapper.MenuMapper;
import net.xxcl.kygl.pojo.Criteria;
import net.xxcl.kygl.pojo.Menu;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional()
public class MenuService {
    @Autowired
    private MenuMapper menuMapper;

    private static final Logger logger = LoggerFactory.getLogger(MenuService.class);

    @Transactional(readOnly = true)
    public List<Menu> selectByExample(Criteria example) {
        return this.menuMapper.selectByExample(example);
    }

    /**
     * 根据角色获取菜单
     * @param roles
     * @return
     */
    @Transactional(readOnly = true)
    public List getMainMenuListByRoles(List<Integer> roles,int level){
        Criteria criteria = new Criteria();
        criteria.put("roles",roles);
        //导航菜单不需要把页面按钮列出
        criteria.put("isbutton",0);
        List<Menu> menus = menuMapper.getMenuByRoles(criteria);
        Menu menu = null;
        //判断哪个是根节点,level是传过来的节点id值，表示查询哪个节点下的数据
        for (Menu m:menus){
            if (m.getParentId()==level){
                menu = m;
                break;
            }
        }
        if(menus.size()>0){
            Map<String,Object> map = menuSimpleTree(menus,menu);
            List list = new ArrayList();
            list.add(map);
            return list;
        }
        return null;
    }

    /**
     * 获取角色对应的某子菜单id下的一级菜单（不包括再下一层菜单）
     * @param roles
     * @return
     */
    @Transactional(readOnly = true)
    public List getLevelMenus(List<Integer> roles,int parentId){
        Criteria criteria = new Criteria();
        criteria.put("roles",roles);
        //导航菜单不需要把页面按钮列出
        criteria.put("isbutton",0);
        //第一级
        criteria.put("parentId",parentId);
        return menuMapper.getMenuByRoles(criteria);
    }

    /**
     * 根据所有菜单用于关联角色
     * @param
     * @return
     */
    @Transactional(readOnly = true)
    public List getAllMenus(int level){
        List<Menu> menus = menuMapper.selectByExample(new Criteria());
        Menu menu = null;
        //判断哪个是根节点,level是传过来的节点id值，表示查询哪个节点下的数据
        for (Menu m:menus){
            if (m.getParentId()==level){
                menu = m;
                break;
            }
        }
        if(menus.size()>0){
            Map<String,Object> map = menuSimpleTree(menus,menu);
            List list = new ArrayList();
            list.add(map);
            return list;
        }
        return null;
    }

    /**
     * 根据主键查找
     * @return
     */
    @Transactional(readOnly = true)
    public Menu selectById(Integer id){
        return menuMapper.selectByPrimaryKey(id);
    }

    @Transactional(readOnly = true)
    private Map<String,Object> menuSimpleTree(List<Menu> menus,Menu menu){  //生成菜单树
        Map<String,Object> map = new HashMap<String, Object>();
        if(getChildList(menus,menu).size()>0){
            map.put("id",menu.getId());
            map.put("qtip",menu.getQtip());
            map.put("text",menu.getName());
            map.put("parentId",menu.getParentId());
            map.put("icon",menu.getImage());
            map.put("expanded",true);
            List childList = getChildList(menus,menu);
            List list = new ArrayList();
            Iterator it = childList.iterator();
            while(it.hasNext()){
                Menu m = (Menu)it.next();
                list.add(menuSimpleTree(menus, m));
            }
            map.put("children",list);
        }else{
            map.put("id",menu.getId());
            map.put("qtip",menu.getQtip());
            map.put("text",menu.getName());
            map.put("parentId",menu.getParentId());
            map.put("icon",menu.getImage());
            map.put("url",menu.getUrl());
            map.put("leaf",true);
        }
        return map;
    }

    @Transactional(readOnly = true)
    private List getChildList(List<Menu> list, Menu menu){  //得到子节点列表
        List li = new ArrayList();
        Iterator it = list.iterator();
        while(it.hasNext()){
            Menu m = (Menu)it.next();
            if(m.getParentId()==menu.getId()){
                li.add(m);
            }
        }
        return li;
    }
}